package com.example.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.system.entity.DO.ExamQuestionDO;
import com.example.system.entity.VO.QuestionVO;
import com.example.system.mapper.ExamQuestionMapper;
import com.example.system.service.IExamQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

@Service
public class ExamQuestionServiceImpl extends ServiceImpl<ExamQuestionMapper, ExamQuestionDO> implements IExamQuestionService {
    @Autowired
    private ExamQuestionMapper examQuestionMapper;


    @Override
    public boolean saveQuestion(List<ExamQuestionDO> questionList) {
        return super.saveBatch(questionList);
    }

    @Override
    public List<Long> removeDuplication(Long examId, List<Long> questionIds) {
        LambdaQueryWrapper<ExamQuestionDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(ExamQuestionDO::getQuestionId)
                .eq(ExamQuestionDO::getExamId, examId);
        List<Long> existsIds = examQuestionMapper.selectList(wrapper)
                .stream().map(ExamQuestionDO::getQuestionId).toList();
        questionIds.removeAll(existsIds);
        questionIds = new HashSet<>(questionIds).stream().toList();
        return questionIds;
    }

    @Override
    public List<QuestionVO> selectExamQuestionList(Long examId) {
        return examQuestionMapper.selectExamQuestionList(examId);
    }

    @Override
    public int questionDelete(Long examId, Long questionId) {
        LambdaQueryWrapper<ExamQuestionDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExamQuestionDO::getExamId, examId)
                .eq(ExamQuestionDO::getQuestionId, questionId);
        return examQuestionMapper.delete(wrapper);
    }

    @Override
    public void deleteByExamId(Long examId) {
        examQuestionMapper.deleteById(examId);
    }

    @Override
    public Long selectQuestionCount(Long examId) {
        LambdaQueryWrapper<ExamQuestionDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExamQuestionDO::getExamId, examId);
        return examQuestionMapper.selectCount(wrapper);
    }
}
